package br.com.mundodaprata.store.data.gen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import br.com.mundodaprata.store.data.jdbc.NeighborhoodRepositoryJDBC;
import br.com.mundodaprata.store.model.City;
import br.com.mundodaprata.store.model.Neighborhood;

public class NeighborhoodGen implements DataGen {
   
   @Override
   public void gen() throws Exception {
      List<Neighborhood> neighborhoods = new ArrayList<Neighborhood>();
      
      File file = new File("C:\\java\\neighborhoods.txt");
      FileReader fr = new FileReader(file);
      BufferedReader reader = new BufferedReader(fr);
      String line;
      
      while ((line = reader.readLine()) != null) {
         String[] data = line.split(";");
         
         Neighborhood neighborhood = new Neighborhood();
         neighborhood.setCode(Long.parseLong(data[0]));
         neighborhood.setState(data[1]);
         
         City city = new City();
         city.setCode(Long.parseLong(data[2]));
         
         neighborhood.setCity(city);
         neighborhood.setName(data[3]);
         
         if (data.length == 5) {
            neighborhood.setAcronym(data[4]);
         }
         
         neighborhoods.add(neighborhood);
      }
      
      NeighborhoodRepositoryJDBC jdbc = new NeighborhoodRepositoryJDBC();
      jdbc.insert((Neighborhood[]) neighborhoods.toArray(new Neighborhood[neighborhoods.size()]));
      jdbc.closeConnection();
   }
   
}